package com.koron.ticket.work.worklocation.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.work.worklocation.bean.WorkLocationBean;
import com.koron.ticket.work.worklocation.mapper.WorkLocationMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkLocationService {

    public void batchInsertOnTicketId(SessionFactory factory, List<WorkLocationBean> list, String ticketId) {
        WorkLocationMapper worklocationmapper = factory.getMapper(WorkLocationMapper.class);
        list.stream().forEach(l->{
            l.setId(CodeTools.getCode32());
            l.setWorkTicketId(ticketId);
        });
        worklocationmapper.batchInsert(list);
    }

    public void batchInsert(SessionFactory factory, List<WorkLocationBean> list) {
        WorkLocationMapper worklocationmapper = factory.getMapper(WorkLocationMapper.class);
        worklocationmapper.batchInsert(list);
    }

    public void deleteByWorkTicketId(SessionFactory factory, String workTicketId) {
        WorkLocationMapper worklocationmapper = factory.getMapper(WorkLocationMapper.class);
        worklocationmapper.deleteByWorkTicketId(workTicketId);
    }

    public List<WorkLocationBean> getList(SessionFactory factory, String workTicketId) {
        WorkLocationMapper worklocationmapper = factory.getMapper(WorkLocationMapper.class);
        List<WorkLocationBean> workMemberList = worklocationmapper.queryByWorkTicketId(workTicketId);
        return workMemberList;
    }

    /**
     * 逗号分割获取地点
     * @param factory
     * @param workTicketId
     * @return
     */
    public String getLocationStr(SessionFactory factory, String workTicketId) {
        List<WorkLocationBean> list = this.getList(factory, workTicketId);
        if (CollUtil.isNotEmpty(list)) {
            List<String> collect = list.stream().map(WorkLocationBean::getLocationName).collect(Collectors.toList());
            String locationstr = StrUtil.join(",", collect);
            return locationstr;
        }
        return "";
    }
}
